/*
   Copyright (c) 2010 Bryce Alcock

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.com

 */
package com.ecocrypt.proxy.forwarder.gui;

import com.ecocrypt.timeutils.Timeformatting;
import java.awt.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

/**
 *
 * @author bryce.alcock
 */
public class ConnectionTableCellRenderer extends DefaultTableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        //  Invoke the parent Cell Renderer.
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        if (value instanceof Date) {
            // if the field is an instance of DATE, we will modify the value of the text.
            // Use SimpleDateFormat class to get a formatted String from Date object.
            String strDate = new SimpleDateFormat("HH:mm:ss SSS").format((Date) value);
            // Sorting algorithm will work with model value. So you dont need to worry
            // about the renderer's display value.
            this.setText(strDate);
        }
        // Column1 is the Durration Column.
        // the format will be changed to Days, Hours, Minutes, sec, ms in the following form
        //  dd - hhH mmM ssS SSSMS where non-present values will be omitted.
        if(column==1){
            long dur = ((Long)value).longValue();
            StringBuilder sb = Timeformatting.durationToString(dur);
            this.setText(sb.toString());
        }
        return this;
    }

}
